<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@page import="com.hnas.core.util.PropertyUtil"%>
<%@page import="com.hnas.core.dto.DataBean"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>特权信息</title>
<script type="text/javascript">
</script>
</head>
<body>
	<!-- 标题 -->
	<div class="titleBar m_b_20">
		<div class="tit fl b_l_e74a5f">
			<h3 class="cn">特权信息</h3>
			<span class="en">pr information</span>
		</div>
		<div class="fr"></div>
	</div>
	<!-- end 标题 -->

	<form method="post" action="${pageContext.request.contextPath }/backend/vip/privilege/search" name="searchForm"  >
  	<input type="hidden" name="pageNo" value="${dto.pageNo}">
  	<input type="hidden" name="totalCount" value="${dto.totalCount}">
				<!-- 查询 -->
			                <div class="box_03 p_20 m_b_20">
			                	<table class="tb_style02" width="100%">
	                				<%-- <tr>
	                					<th>姓名</th>
	                					<td><input name="name" value="${dto.name}" style="width:200px;" class="input_style2" type="text" placeholder=""></td>
	                					<th>账号</th>
	                					<td><input name="account" value="${dto.account}" style="width:200px;" class="input_style2" type="text" placeholder=""></td>
	      
	                				</tr> --%>
	                			
	                				<tr>
	                					<th>每页显示</th>
	                					<td colspan="5">
	                						<input style="width:50px;" class="input_style2" type="text" placeholder="" id="pageSize" name="pageSize" value="${dto.pageSize}"> 条
	                						<span style="margin-left:170px;">
	                							<input style="width:110px;" class="btn_brown02 m_r_12" type="button" name="searchData" id="searchData" value="查  询">
	                							<input style="width:110px;" class="btn_brown02 m_r_12" type="button" name="addData" id="addData" value="添 加">
	                						</span>
	                					</td>
	                				</tr>
		                		</table>
			                </div>
			                <!-- end 查询 -->
				
</form>
<%
boolean incDisplayTable = true;
boolean incEditTable = false;
boolean incUploadDiv = true;
boolean incBatchDelete = false;
boolean incDisplayStatus =  true;
String uploadTmp="resources.xls";
%>

<div class="m_b_20">
	<table class="tb_style05" width="100%" style="table-layout:fixed;">
		<thead>
		<tr>
			<c:forEach items="${beans}" var="o">
			<c:if test="${o.display && !o.hidden}"><th>${o.title}</th></c:if>
			</c:forEach>
			<th width="100">操作</th>
		</tr>
		</thead>
		<tbody>
			<c:forEach items="${pg.list}" var="o">
			<tr>
				<c:forEach items="${beans}" var="o1">
					<c:if test="${o1.display && !o1.hidden}">
					<td style="word-wrap:break-word;"><%=String.valueOf(PropertyUtil.getValue(pageContext.getAttribute("o"),((DataBean)pageContext.getAttribute("o1")).getField())).replace("false","否").replace("true","是")%></td></c:if>
				</c:forEach>
					<td>
						<a href="#" class="c_3366cc btn editData" id="${o.id }">编辑</a>&nbsp;&nbsp;
						<a href="#" class="c_3366cc btn deleteData" id="${o.id }">删除</a>
					</td>
			</tr>
			</c:forEach>
		</tbody>
	</table>
	<div class="searpage" align="right"><div>${pg.pageStr}</div></div><br/>
</div>

<!-- 添加start -->
<div class="editDataDiv" id="editDIV" title="编辑特权信息" style="display: none;" >
	<form action="#" id="editFm" method="post" name="editFm" class="editFm" >
		<input type="hidden" id="dealId" name = "id" value=""/>
		<table >
			<tr>
 				<td width="25%" align="left"> 
 					<label>特权名称：</label>
 				</td>
 				<td><input type="text" id="privilegeNameInput" name="name"> </td> 
  			</tr>
  			<tr>
 				<td width="25%" align="left"> 
 					<label>优惠方式：</label>
 				</td>
 				<td><select id="typeSelect" name="type" style="width: 100px;" class="select ui-widget-content ui-corner-all input_style2">
	 					<c:forEach items="${typeMap }" var="o">
	 						<option value="${o.key }" >${o.value }</option>
	 					</c:forEach>
	 				</select>
 				</td> 
  			</tr>
  			<tr>
 				<td width="25%" align="left"> 
 					<label>优惠数额：</label>
 				</td>
 				<td><input type="text" id="valueInput" name="transVal" style="width:100px;"><label id="unit">折</label></td> 
  			</tr>
			<tr>
 				<td width="25%" align="left"> 
 					<label>是否可用：</label>
 				</td>
 				<td>
 				<input type="hidden" id="disabled" name="disabled" />
 				<input type="radio" id="y" name="checkRadio" checked="checked" ><label for="y">是</label>
 				<input type="radio" id="n" name="checkRadio" ><label for="n">否</label> </td> 
  			</tr>
  			<tr>
 				<td width="25%" align="left"> 
 					<label>描述：</label>
 				</td>
 				<td><textarea id="descInput" name="description" rows="3" style="width:200px;"></textarea> </td> 
  			</tr>
			<!-- <tr>
      			<td colspan="2">
          			<p class="validateTips_deal" align="center"></p>
      			</td>
    		</tr> -->
		</table>
	</form>
</div>
<!-- 添加end -->
<script>
function gotoJumpPage(){
		var jumpNum= $("#jumpNum").val();
		$("#search_page_size").val($("#id_page_size").val());
		gotoPage(jumpNum);
		return false;
	}
	$(function(){
		/** 添加 **/	 
		$("#addData").click(function(){
			doReset(document.editFm);
			$("#editDIV").dialog("open");
		});

		/** 编辑 **/
		$(".editData").click(function(){
			var id = $(this).attr("id");
			var editURL = "${pageContext.request.contextPath }/backend/vip/privilege/show/" + id;
			$.ajax({
				url:editURL,
				type: 'POST',
				error: function(XMLHttpRequest, textStatus, errorThrown){
					//alert('加载数据异常。' + textStatus + "/" + errorThrown);
					 $.dialog.alert({title:"保存",content:"加载数据异常。"});
				},
				success: function(data){
					fillDIV(data);
				}
			});
			$("#editDIV").dialog("open");
		});
		$("#editDIV").dialog({
			autoOpen : false,
			bgiframe: true, 
			height : 370,
			width : 350,
			modal : true,
			resizable :false,
	       	hide: '',
	        show:'',
	        zIndex:1000,
			buttons : {
				"保 存" : function() {
					var bValid = doValid();
					if(bValid){
						var isAdd = ($("#dealId").val() == null) || ($.trim($("#dealId").val()) == "")||($.trim($("#dealId").val()) == "0");
						var myAddUrl = "${pageContext.request.contextPath }/backend/vip/privilege/add";
						var mySaveUrl = "${pageContext.request.contextPath }/backend/vip/privilege/save";
						var url = isAdd?myAddUrl:mySaveUrl;
						if($("#y").is(":checked")){
							$("#disabled").val(1);
						}else{
							$("#disabled").val(0);
						}
						$.ajax({
							url:url,
							type: 'POST',
							data: $("#editFm").serialize(),
							error: function(XMLHttpRequest, textStatus, errorThrown){
							//alert('保存异常。' + textStatus + "/" + errorThrown);
							 $.dialog.alert({title:"保存",content:"保存异常"});
							},
							success: function(data){
								if(data.result==1) {
									$("#editDIV").dialog("close");
									if (isAdd) {
										//alert('添加成功.');
										 $.dialog.alert({title:"保存",content:"添加成功"});
									}
									else {
										//alert('修改成功.');
										$.dialog.alert({title:"保存",content:"修改成功"});
									}
									query();
								} else {
									$.dialog.alert({title:"保存",content:"保存失败："+ data.errorMessage});	
								}
							}
						});  
					}
				}, 
				"取 消" : function() {
					$(this).dialog("close");			
				}
			},
			close : function() {
				$(this).dialog("close");
			}
		});
		function fillDIV(data){
			console.log(data);
			$("#dealId").val(data.id);
			$("#privilegeNameInput").val(data.name);
			$("#valueInput").val(data.transVal);
			$("#typeSelect option").each(function(){
				if($(this).val() == data.type){
					$(this).attr("selected","selected");
				}
			})
			if(data.type == 0){
				$("#unit").text("折");
			}else if(data.type == 1){
				$("#unit").text("元");
			}
			if(data.disabled == 0){
				$("#n").attr("checked","checked");	
			}else{
				$("#y").attr("checked","checked");	
			}
			$("#descInput").val(data.description);
			
			
		}
		function doValid(){
			//TODO
			return true;
		}
		$("#typeSelect").change(function(){
			var val = $(this).val();
			if(val == 0){
				$("#unit").text("折");
			}else if(val == 1){
				$("#unit").text("元");
			}
		})
		$("#searchData").click(function(){
			
			/** 校验输入页数是否非法  **/
			var pageSize=$("#pageSize").val();
			var reg = new RegExp("^[0-9]*$");
			if($.trim(pageSize)!=""){
				if(!reg.test($.trim(pageSize))){
					alert("每页显示数请输入非负整数");
	 				return false;
				}
			}
			if($.trim(pageSize)==""){
				alert("每页显示数不应许为空");
				return false;
			}
			
			doSubmit();
		});
	});
	</script>
</body>
</html>